package com.hengxinguotong.zhihuichengjian.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.text.format.Time;
import android.util.Config;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by admin on 2016/9/21.
 */
@SuppressWarnings("deprecation")
@SuppressLint("SimpleDateFormat")
public class LogUtil {

    private static boolean DBG = Config.RELEASE;// 打印日志
    //	 private static final boolean DBG = Config.DEBUG;//不打印日志
    private static final int sDay = 7;//设置默认日志文件保存几天
    //设置测试环境打印日志，生产不打印
    static {
		/*if (URLConfig.REGEDIT_URL.equals("/hhvideo/app/server/AndroidServlet")) {
			DBG = true;
		} else {
			DBG = false;
		}*/
    }

    private static FileOutputStream sDBGOutputStream = null;

    private LogUtil() {

    }

    /**
     * Send a log message.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     */
    public static void v(String tag, String msg) {
        if (DBG) {
            Log.v(tag, msg);
        }
    }

    /**
     * Send a log message and log the exception.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     * @param tr
     *            An exception to log
     */
    public static void v(String tag, String msg, Throwable tr) {
        if (DBG) {
            Log.v(tag, msg, tr);
        }
    }

    /**
     * Send a log message.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     */
    public static void d(String tag, String msg) {
        if (DBG) {
            Log.d(tag, msg);
        }
    }

    /**
     * Send a log message and log the exception.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     * @param tr
     *            An exception to log
     */
    public static void d(String tag, String msg, Throwable tr) {
        if (DBG) {
            Log.d(tag, msg, tr);
        }
    }

    /**
     * Send an  log message.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     */
    public static void i(String tag, String msg) {
        if (DBG) {
            Log.i(tag, msg);
        }
    }

    /**
     * Send a  log message and log the exception.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     * @param tr
     *            An exception to log
     */
    public static void i(String tag, String msg, Throwable tr) {
        if (DBG) {
            Log.i(tag, msg, tr);
        }
    }

    /**
     * Send a  log message.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     */
    public static void w(String tag, String msg) {
        if (DBG) {
            Log.w(tag, msg);
        }
    }
    public static void w(String msg) {
        if (DBG) {
            Log.w("erro", msg);
        }
    }

    /**
     * Send a  log message and log the exception.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     * @param tr
     *            An exception to log
     */
    public static void w(String tag, String msg, Throwable tr) {
        if (DBG) {
            Log.w(tag, msg, tr);
        }
    }

    /*
     * Send a {@link #WARN} log message and log the exception.
     *
     * @param tag Used to identify the source of a log message. It usually
     * identifies the class or activity where the log call occurs.
     *
     * @param tr An exception to log
     */
    public static void w(String tag, Throwable tr) {
        if (DBG) {
            Log.w(tag, tr);
        }
    }

    /**
     * Send an  log message.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     */
    public static void e(String tag, String msg) {
        if (DBG)
            Log.e(tag, msg);
        // System.out.print(tag+":"+msg);
    }

    /**
     * Send a  log message and log the exception.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     * @param tr
     *            An exception to log
     */
    public static void e(String tag, String msg, Throwable tr) {
        if (DBG)
            Log.e(tag, msg, tr);
    }

    /**
     * Write a log message to trace file under the sdcard root directory.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     */
    public synchronized static void f(String tag, String msg) {
        if (DBG) {

            i(tag, msg);
            toFile(Log.DEBUG, tag, msg);

        }
    }

    /**
     * Write a log message to trace file under the sdcard root directory, and
     * print a log by cmd line.
     *
     * @param priority
     *            The priority/type of this log message
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     */
    public synchronized static void f(int priority, String tag, String msg) {
        if (DBG) {
            toFile(priority, tag, msg);
        }
    }


    /**
     * Write a log message to trace file under the sdcard root directory.
     *
     * @param tag
     *            Used to identify the source of a log message. It usually
     *            identifies the class or activity where the log call occurs.
     * @param msg
     *            The message you would like logged.
     */
    private synchronized static void toFile(int priority, String tag, String msg) {
        if (sDBGOutputStream == null) {
            initDBGOutputStream();
        }

        if (sDBGOutputStream != null) {
            try {
                Time time = new Time();
                time.setToNow();
                String nowDate = new SimpleDateFormat("yyyy_MM_dd HH:mm:ss")
                        .format(new Date());
                String msgdata = "******************" + nowDate
                        + "******************\n" + " " + "/" + tag + "-- "
                        + msg + "\r\n";
                sDBGOutputStream.write(msgdata.getBytes());
                sDBGOutputStream.flush();

            } catch (Exception e) {
                e.printStackTrace();
                try {
                    sDBGOutputStream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                sDBGOutputStream = null;
            }
        }

    }

    private static void initDBGOutputStream() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            File savePath;

            savePath = new File(Environment.getExternalStorageDirectory()
                    + "/logs/");
            if (!savePath.exists()) {
                savePath.mkdirs();
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String nowDate = sdf.format(new Date());
            File file = new File(Environment.getExternalStorageDirectory(),
                    "/logs/CABX_log" + nowDate + ".txt");
            if (file.exists()) {
                for(String fileStr :file.getParentFile().list()){
                    Date fileD = null;
                    try {
                        fileD = sdf.parse(fileStr.substring(fileStr.length()-12,fileStr.length()-4));
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                    if (fileD.before(getLastWeek())) {
                        if(new File(savePath,"/"+fileStr).delete()){
                            i("logs", "日志文件删除成功");
                        }else{

                            i("logs", "日志文件删除失败");
                        }

                    }
                }
            }

            try {
                sDBGOutputStream = new FileOutputStream(file, true);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    public static Date getLastWeek(){

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)-sDay);
        return calendar.getTime();
    }
	/*
	 * @Override protected void finalize() throws Throwable { if
	 * (sDBGOutputStream != null) { try { sDBGOutputStream.close(); } catch
	 * (IOException e1) { e1.printStackTrace(); } } super.finalize(); }
	 */

    public static void toast(Context ctx, String msg) {
        if (DBG) {
            Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
        }
    }

}
